import java.util.StringTokenizer;

class P10130 {

	public static void main(String[] args) {
		int cases = Integer.parseInt(readLn());
		for (int k = 0; k < cases; k++) {
			process(Integer.parseInt(readLn()));
		}
	}

	private static void process(int numObjects) {
		int[] objectsW = new int[numObjects];
		int[] objectsV = new int[numObjects];
		
		for (int k = 0; k < numObjects; k++) {
			StringTokenizer st = new StringTokenizer(readLn());
			objectsV[k] = Integer.parseInt(st.nextToken());
			objectsW[k] = Integer.parseInt(st.nextToken());
		}
		
		int counter = 0;
		int numberOfPeople = Integer.parseInt(readLn());
		for (int k = 0; k < numberOfPeople; k++) {
			counter += ks(objectsW, objectsV, Integer.parseInt(readLn())); 
		}
		System.out.println(counter);
		
		
	}

	private static int ks(int[] array, int[] values, int mw) {
		int[][] table = new int[array.length+1][mw+1];
		for (int i=0;i<=array.length ;i++) table[i][0] = 0;
		for (int w=0;w<=mw;w++) table[0][w] = 0;

		for (int i=1;i<=array.length;i++) {
		  for (int w=1;w<=mw;w++) {
		    if (array[i-1] > w)
		      table[i][w] = table[i-1][w];
		    else
		      table[i][w] = max(table[i-1][w] , table[i-1][w-array[i-1]]+values[i-1]);
		  }
		}
		
		return table[array.length][mw];
	}

	private static int max(int i, int j) {
		return i >= j ? i : j;
	}

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}

}
